<!DOCTYPE html>
<html>
<head>
    <title>Hello World worker</title>
    <meta charset="UTF-8">
    <script type="text/javascript">
        var gworker = null;
        function create_worker() 
        {
            console.log(navigator);
            console.log(navigator.language);
            console.log(navigator.onLine);

            gworker = new Worker('hwww1.js');

            gworker.addEventListener('message', function(e) {
                console.log('Worker said: ', e.data);
                document.getElementById('status').innerHTML +='<br/>'+ 'Worker said: '+ e.data;
                console.log(window);
                window.close();
            }, false);
//            var count = 0;
//            while(count < 50){
                gworker.postMessage('Hello World'); // Send data to our worker.
//                count++;
//            }
        }
            
        function worker_msg()
        {
            gworker.postMessage('Hello World'); // Send data to our worker.
        }
        
        function worker_close()
        {
            gworker.terminate();
            //gworker = null;
        }
        
        function create_nested_worker() 
        {
            console.log(navigator);
            console.log(navigator.language);
            console.log(navigator.onLine);
            console.log("Wrkr1 " + navigator.languages.length);
            for(var i = 0; i < navigator.languages.length; i++)
                console.log("Wrkr3:lang[" + i + "]-" + navigator.languages[i]);
            
            var ngworker = new Worker('hwww2.js');

            ngworker.addEventListener('message', function(e) {
                console.log('Worker said: ', e.data);
                document.getElementById('status').innerHTML +='<br/>'+ 'Worker said: '+ e.data;
            }, false);

            ngworker.postMessage('Hello World'); // Send data to our worker.
        }

        function create_nested_worker_for_IE10() 
        {
            console.log(navigator);
//            console.log(navigator.language);
            console.log(navigator.onLine);
/*            console.log("Wrkr1 " + navigator.languages.length);
            for(var i = 0; i < navigator.languages.length; i++)
                console.log("Wrkr3:lang[" + i + "]-" + navigator.languages[i]);
  */          
            var ngworker = new Worker('hwww2.js');

            ngworker.addEventListener('message', function(e) {
                console.log('Worker said: ', e.data);
                document.getElementById('status').innerHTML +='<br/>'+ 'Worker said: '+ e.data;
            }, false);

            ngworker.postMessage('Hello World'); // Send data to our worker.
        }
        
        
        function test_navigator_cpu()
        {
            console.log('Main thread : ' + navigator.hardwareConcurrency);
            var ngworker = new Worker('hwww3.js');

            ngworker.addEventListener('message', function(e) {
                console.log('Worker said: no of cores : ', e.data);
                document.getElementById('status').innerHTML +='<br/>'+ 'Worker said: no of cores : '+ e.data;
            }, false);

            ngworker.postMessage('No of cores'); // Send data to our worker.
        }
        
        function hello_shared_worker()
        {
            var worker = new SharedWorker("hwww4.js");
            
            worker.port.addEventListener("message",
                function(event) {
                    console.log(event.data);
                }
            , false
            );
            
            worker.port.start();
        }
        
        function open_worker_close_window() 
        {
            console.log(navigator);
            console.log(navigator.language);
            console.log(navigator.onLine);

            var worker = new Worker('hwww5.js');

            worker.addEventListener('message', function(e) {
                console.log('Worker said: ', e.data);
                document.getElementById('status').innerHTML +='<br/>'+ 'Worker said: '+ e.data;
                console.log('Closing window now...');
            }, false);

            worker.postMessage('Hello World'); // Send data to our worker.
        }
        
        function create_multiple_workers()
        {
            var worker_array = [];
            for (var i = 0; i < 10 ;i++){
                console.log('creating worker no ' + i);
                worker_array[i] = new Worker('hwww5.js');

                worker_array[i].addEventListener('message', function(e) {
                    console.log('Worker said: ', e.data);
                    document.getElementById('status').innerHTML +='<br/>'+ 'Worker said: '+ e.data;
                    console.log('Closing window now...');
                }, false);

                worker_array[i].postMessage('Hello World'); // Send data to our worker.
            }            
        }
        
        function check_dedicated_worker_nav_lang()
        {
            console.log("Window obj values");
            console.log(navigator.language);
            console.log(navigator.onLine);
            var worker = new Worker('hwww6.js');
            worker.postMessage('Hello World'); // Send data to our worker.

        }
        
        function check_shared_worker_nav_lang()
        {
            
        }
        
        function fire_50_msgs_to_worker()
        {
            var nworker = new Worker('hwww7.js');

            nworker.addEventListener('message', function(e) {
                console.log('Worker said: ', e.data);
            }, false);
            var count = 0;
            while(count < 50){
                nworker.postMessage('Hello World'); // Send data to our worker.
                count++;
            }            
        }
        
        function fire_back_to_back_timers()
        {
            var nworker = new Worker('hwww8.js');

            nworker.addEventListener('message', function(e) {
                console.log('Worker said: ', e.data);
            }, false);
            nworker.postMessage('Hello World'); // Send data to our worker.
        }
        
        
        
    </script>
</head>
<body>
    <input type="button" onclick="javascript:check_dedicated_worker_nav_lang();" value="Dedicated Web Worker navigator.language check !!"></input><BR/>
    <input type="button" onclick="javascript:check_shared_worker_nav_lang();" value="Shared Web Worker navigator.language check !!"></input><BR/>
    <input type="button" onclick="javascript:create_worker();" value="Create Web Worker!!"></input><BR/>
    <input type="button" onclick="javascript:worker_msg();" value="Hello, to Web Worker!!"></input><BR/>
    <input type="button" onclick="javascript:worker_close();" value="Close Web Worker!!"></input><BR/>
    <input type="button" onclick="javascript:open_worker_close_window();" value="Close window now."></input><BR/>
    <input type="button" onclick="javascript:create_nested_worker();" value="Create Nested Web Worker!!"></input><BR/>
    <input type="button" onclick="javascript:create_nested_worker_for_IE10();" value="Create Nested Web Worker for IE10!!"></input><BR/>
    <input type="button" onclick="javascript:test_navigator_cpu();" value="Check Web Worker CPU!!"></input><BR/>
    <input type="button" onclick="javascript:hello_shared_worker();" value="Hello, to Shared Web Worker!!"></input><BR/>    
    <input type="button" onclick="javascript:create_multiple_workers();" value="Create multiple Web Workers!!"></input><BR/>    
    <input type="button" onclick="javascript:fire_50_msgs_to_worker();" value="Send 50 msgs to Worker!!"></input><BR/>    
    <input type="button" onclick="javascript:fire_back_to_back_timers();" value="Fire back to back timers in Worker!!"></input><BR/>    
    <div id="status">
        
    </div>
</body>
</html>